package com.ctos.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ctos.common.Result;
import com.ctos.dto.DishDto;
import com.ctos.entity.Dish;
import com.ctos.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dish")
public class DishController {
    @Autowired
    private DishService dishService;

    @Autowired
    private CacheManager cacheManager;

    /**
     * 分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public Result<Page<DishDto>> pageQuery(Integer page, Integer pageSize, String name){
        return  dishService.foodPage(page, pageSize, name);

    }

    /**
     * 新增菜品
     * @param dishDto
     * @return
     */
    @PostMapping
    @CacheEvict(value = "dishCache",allEntries = true)
    public Result<String> save(@RequestBody DishDto dishDto){
        dishService.saveWithFlavor(dishDto);
        return Result.success("success");
    }

    /**
     * 回显数据
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public Result<DishDto> echo(@PathVariable Long id){
        DishDto dishDto = dishService.getDishDto(id);

        return Result.success(dishDto);
    }

    /**
     * 修改菜品
     * @param dishDto
     * @return
     */
    @PutMapping
    @CacheEvict(value = "dishCache",allEntries = true)
    public Result<String> update(@RequestBody DishDto dishDto){
        dishService.updateDishAndDishFlavor(dishDto);



        return Result.success("success");
    }

    /**
     * 删除菜品
     * @param ids
     * @return
     */
    @DeleteMapping()
    @CacheEvict(value = "dishCache",allEntries = true)
    public Result<String> delete(@RequestParam("ids") Long[] ids){
        dishService.delete(ids);
        return Result.success("success");
    }

    /**
     * 停售
     * @param ids
     * @return
     */
    @PostMapping("/status/0")
    @CacheEvict(value = "dishCache",allEntries = true)
    public Result<String> downStatus(@RequestParam("ids") Long[] ids){

        dishService.modifyStatus(0, ids);
        return Result.success("success");
    }

    /**
     * 起售
     * @param ids
     * @return
     */
    @PostMapping("/status/1")
    @CacheEvict(value = "dishCache",allEntries = true)
    public Result<String> openStatus(@RequestParam("ids") Long[] ids){
        dishService.modifyStatus(1, ids);
        return Result.success("success");
    }

    /**
     * 获取列表
     * @param dish
     * @return
     */
    @GetMapping("/list")
    @Cacheable(value = "dishCache",key = "#dish.categoryId + '_'+ #dish.status")
    public Result<List<DishDto>> dishList( Dish dish){
        List<DishDto> dishes = dishService.dishList(dish);
        return Result.success(dishes);
    }

}
